﻿var htzTimeout = 0;

function Minimap(_mapFrame, _mapElementId, _container, _initscale) {
    this.mapElement = document.getElementById(_mapElementId);
    this.container = _container;
    this.initscale = _initscale;

    //============ Required from main script ============
    this.mapAgentUrl = "";
    this.sessionId = "";
    this.mapName = "";
    this.mapDefinition = "";
    //===================================================

    this.initialDisplayed = true;
    this.positionTop = '10px';
    this.positionLeft = '10px';
    this.width = '150px';
    this.height = '130px';
    this.opacity = 0.9;
    this.backgroundColor = '#FFFFFF';
    this.borderWidth = "2px";
    this.borderColor = '#C0C0C0';
    this.imageUrl = "";
    var mm = this;
    if (typeof (_mapFrame) != 'undefined' && _mapFrame != null) {
        this.mapFrame = _mapFrame;

        //originOnMapLoading = this.mapFrame.OnMapLoading;
        this.mapFrame.RefreshMinimap = function() {
            mm.RefreshMinimap();
        }
    }
    this.centerX = this.mapFrame.extX1 + (this.mapFrame.extX2 - this.mapFrame.extX1) / 2;
    this.centerY = this.mapFrame.extY2 + (this.mapFrame.extY1 - this.mapFrame.extY2) / 2;
    this.extLayer = "";
    this.isLoaded = false;
    return this;
}

Minimap.prototype.ShowMinimap = function() {
    if (typeof (this.container) != 'undefined' && this.container != null && typeof (this.container.tagName) != 'undefined' && this.container.tagName.toLocaleLowerCase() == 'div') {
        this.width = this.container.style.width;
        this.height = this.container.style.height;
    }
    else {
        this.CreateContainer();
    }
    this.RefreshMinimap();
    this.isLoaded = true;
};

Minimap.prototype.CreateContainer = function() {

    if (typeof (this.width) == 'undefined' || typeof (this.height) == 'undefined') return;

    var div = document.createElement('DIV');
    div.width = this.width;
    div.height = this.height;

    div.style.display = this.initialDisplayed ? 'block' : 'none';
    //div.style.backgroundImage = 'url(' + imageUrl + ')';
    div.style.cssText += ";background-repeat:no-repeat;background-position:center center;background-color:" + this.backgroundColor;
    div.style.zIndex = 3;

    div.style.position = 'absolute';
    div.style.width = this.width;
    div.style.height = this.height;
    div.style.opacity = this.opacity;
    div.style.borderColor = this.borderColor;
    div.style.borderWidth = this.borderWidth;
    div.style.borderStyle = "solid";
    div.style.filter = 'alpha(opacity=' + this.opacity * 100 + ')';
    div.style.top = this.positionTop;
    div.style.left = this.positionLeft;
    div.setAttribute('id', 'minimap');

    if (this.mapElement != null) {
        this.mapElement.parentNode.insertBefore(div, this.mapElement);
        this.container = div;
    }
};

Minimap.prototype.SetPolygon = function() {
    var mm = this;
    var requestParams = "SESSION=" + sessionId
                  + "&MAPNAME=" + mapName
                  + "&SEQ=" + Math.random()
                  + "&GEOMTEXT=" + generateGeomText(this.mapFrame.extX1, this.mapFrame.extY1, this.mapFrame.extX2, this.mapFrame.extY2);
    var mr = getXmlHttp();

    mr.open("POST", "/mapguide/mapviewernet/createoverviewext.aspx", true);
    mr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    mr.onreadystatechange = function() {
        if (mr.readyState == 4) {
            mm.mapFrame.hiddenLayers = mr.responseText;
            mm.RequestMinimapImage(mr.responseText);
        }
    }
    try { mr.send(requestParams); }
    catch (e) { return; }
};

Minimap.prototype.RequestMinimapImage = function(layerId) {
    this.extLayer = layerId;
    this.container.style.backgroundImage = "url('')";
    this.imageUrl = this.GenerateImageUrl();
    this.container.style.backgroundImage = "url('" + this.imageUrl + "')";
};

Minimap.prototype.GenerateImageUrl = function(imgWidth, imgHeight){
	if(typeof(width) == "undefined" || width == null || typeof(height) == "undefined" || height == null)
	{
		imgWidth = this.width;
		imgHeight = this.height;
	}
	
	return this.mapAgentUrl
		   + "?OPERATION=GETDYNAMICMAPOVERLAYIMAGE"
		   + "&VERSION=2.1.0"
		   + "&SESSION=" + this.sessionId
		   + "&MAPNAME=" + encodeURIComponent(this.mapName)
		   + "&MAPDEFINITION=" + encodeURIComponent(this.mapDefinition)
		   + "&LOCALE=EN"
		   + "&SEQ=" + Math.random()
		   + "&FORMAT=PNG"
		   + "&SETVIEWCENTERX=" + this.centerX
		   + "&SETVIEWCENTERY=" + this.centerY
		   + "&SETVIEWSCALE=" + this.initscale
		   + "&SETDISPLAYDPI=75"
		   + "&SETDISPLAYWIDTH=" + imgWidth
		   + "&SETDISPLAYHEIGHT=" + imgHeight
		   + "&SHOWLAYERS=" + encodeURIComponent(this.extLayer)
		   + "&BEHAVIOR=2";
}

Minimap.prototype.RefreshMinimap = function() {
    if (mapLoading == true) {
        htzTimeout = window.setTimeout("this.RefreshMinimap()", 1500);
        return;
    }
    this.SetPolygon();
    window.clearTimeout(htzTimeout);
};

function getXmlHttp() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function generateGeomText(blX, blY, trX, trY) {
    return "5," + blX + "," + blY + "," + blX + "," + trY + "," + trX + "," + trY + "," + trX + "," + blY + "," + blX + "," + blY;
}